﻿Imports BUS
Imports System.Data.SqlClient

Public Class frmCauHinhCa

    Private da1 As SqlDataAdapter
    Private da2 As SqlDataAdapter
    Private da3 As SqlDataAdapter
    Private ds As New DataSet

    Private Sub UpdateT(ByVal dgv As DataGridView)
        Dim dt As New DataTable
        dt = dgv.DataSource
        Select Case dgv.Name
            Case "dgvcauhinhhc"
                Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da1)
                da1.Update(dt)
            Case "dgvcauhinhtc15"
                Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da2)
                da2.Update(dt)
            Case "dgvcauhinhtc195"
                Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da3)
                da3.Update(dt)
        End Select

    End Sub

    Private Sub frmCauHinhCa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sql As String = "select * from TinhHanhChinh"
        da1 = ProcFuncBus.ExecuteFuncSQL_SqlDataAdapter(sql)
        da1.Fill(ds, "TinhHanhChinh")
        dgvcauhinhhc.DataSource = ds.Tables("TinhHanhChinh")

        sql = "select * from TinhTangCa"
        da2 = ProcFuncBus.ExecuteFuncSQL_SqlDataAdapter(sql)
        da2.Fill(ds, "TinhTangCa")
        dgvcauhinhtc15.DataSource = ds.Tables("TinhTangCa")

        sql = "select * from TinhTangCa21h"
        da3 = ProcFuncBus.ExecuteFuncSQL_SqlDataAdapter(sql)
        da3.Fill(ds, "TinhTangCa21h")
        dgvcauhinhtc195.DataSource = ds.Tables("TinhTangCa21h")
    End Sub

    Private Sub LưuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LưuToolStripMenuItem.Click
        UpdateT(dgvcauhinhhc)
        UpdateT(dgvcauhinhtc15)
        UpdateT(dgvcauhinhtc195)

        MessageBox.Show("Da luu")
    End Sub

    Private Sub XoaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XoaToolStripMenuItem.Click
        Dim digres As DialogResult = MessageBox.Show("Bạn có muốn xóa không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
        If digres = DialogResult.Yes Then
            Dim item As DataGridViewSelectedRowCollection = dgvcauhinhhc.SelectedRows
            For Each row As DataGridViewRow In item
                dgvcauhinhhc.Rows.Remove(row)
            Next

            item = dgvcauhinhtc15.SelectedRows
            For Each row As DataGridViewRow In item
                dgvcauhinhtc15.Rows.Remove(row)
            Next

            item = dgvcauhinhtc195.SelectedRows
            For Each row As DataGridViewRow In item
                dgvcauhinhtc195.Rows.Remove(row)
            Next

            UpdateT(dgvcauhinhhc)
            UpdateT(dgvcauhinhtc15)
            UpdateT(dgvcauhinhtc195)
        Else
            Return
        End If
    End Sub
End Class